﻿using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SelfManagement.Common;
using SelfManagement.Utility.Extensions;
using SelfManagement.Common.Log;

namespace SelfManagement.Log4Net
{
    public class Log4NetLoggerManager : ILoggerManager
    {
        private Dictionary<LogLevel, bool> enabledLevels;

        public Log4NetLoggerManager(string configFile = "log4net.config")
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile.GetPhysicalPath()));
            enabledLevels = new Dictionary<LogLevel, bool>();
            enabledLevels.Add(LogLevel.Debug, true);
            enabledLevels.Add(LogLevel.Error, true);
            enabledLevels.Add(LogLevel.Fatal, true);
            enabledLevels.Add(LogLevel.Info, true);
            enabledLevels.Add(LogLevel.Trace, true);
            enabledLevels.Add(LogLevel.Warn, true);
        }

        public void EnableLogLevel(LogLevel level, bool enabled)
        {
            enabledLevels[level] = enabled;
        }

        public bool IsLogLevelEnabled(LogLevel level)
        {
            return enabledLevels[level];
        }

        public ILogger GetLogger(Type type)
        {
            var log = LogManager.GetLogger(type);
            return new Log4NetLogger(this, log);
        }
    }
}
